package cn.smbms.servlet;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MainServlet extends HttpServlet {

	private BillService bill = new BillService();
	private ProviderService provider = new ProviderService();
	private UserService user = new UserService();

	public MainServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletPath();
		path = path.substring(1, path.length() - 3);
		String[] params = path.split("-");
		if ("service".equals(path) || "doGet".equals(path) || "doPost".equals(path)) {
			response.sendError(500, "请求地址非法");
			return;
		}
		if (params.length == 1) {
			try {
				Method method = this.getClass().getDeclaredMethod(path, HttpServletRequest.class,
						HttpServletResponse.class);
				method.invoke(this, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.sendError(500, "请求地址不存在");
			}
		}
		if (params.length == 2) {
			try {
				Field filed = this.getClass().getDeclaredField(params[0]);
				Object obj = filed.get(this);
				Method m = obj.getClass().getDeclaredMethod(params[1], HttpServletRequest.class,
						HttpServletResponse.class);
				m.invoke(obj, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
